﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EjemploMVC.Models;

namespace EjemploMVC.Controllers
{
    public class ConsultaController : Controller
    {

        private List<ConsultaModels> CrearCursos()
        {

            List<ConsultaModels> lista = new List<ConsultaModels>();
            lista.Add(new ConsultaModels { idCurso = 1, nombreCurso = "C# Developer Junior", encargadoCurso = "Cesar Garrido Lecca Rivera" });
            lista.Add(new ConsultaModels { idCurso = 2, nombreCurso = "Java Developer Junior", encargadoCurso = "Lilian Sotomayor" });
            lista.Add(new ConsultaModels { idCurso = 3, nombreCurso = "VB Developer Junior", encargadoCurso = "Edson Menendes" });
            lista.Add(new ConsultaModels { idCurso = 4, nombreCurso = "Concat", encargadoCurso = "Alberto Burga" });
            lista.Add(new ConsultaModels { idCurso = 5, nombreCurso = "Configacion de Redes locales", encargadoCurso = "ALberto Valiente" });
            return lista;

        }


        //
        // GET: /Consulta/

        public ActionResult Index()
        {
            //Session["lista"] = CrearCursos();

            /*IEnumerable<SelectListItem> items = ((List<ConsultaModels>)Session["lista"])
             .Select(c => new SelectListItem
             {
                 Value = c.idCurso.ToString(),
                 Text = c.nombreCurso
             });*/

            List<ConsultaModels> lista = CrearCursos();
            var model = new ConsultaModels
            {
                // you probably would fetch those from the database 
                Cursos = (lista).Select(c => new SelectListItem{Value = c.idCurso.ToString(), Text = c.nombreCurso})
            };

            return View(model); 

        }


        [HttpPost]
        public ActionResult Index(string idCurso)
        {
            //List<ConsultaModels> lista = (List<ConsultaModels>)Session["lista"];
            //var cc = (from c in lista where c.idCurso == xx select c.encargadoCurso);
            // selectedCountry will contain the code that you could use to  
            // query your database 
            //return View(xx); //RedirectToAction("index",);
            List<ConsultaModels> lista = CrearCursos();
            
            if (idCurso != "")
            {
                var xx = (from c in lista
                          where c.idCurso == Convert.ToInt32(idCurso)
                          select new { c.idCurso, c.nombreCurso, c.encargadoCurso }).First();

                var model = new ConsultaModels
                {
                    // you probably would fetch those from the database 
                    Cursos = (lista).Select(c => new SelectListItem { Value = c.idCurso.ToString(), Text = c.nombreCurso, Selected = c.idCurso == Convert.ToInt32(idCurso) ? true : false }),
                    encargadoCurso = xx.encargadoCurso.ToString(),
                };
                return View(model);
            }
            else
            {
                var model = new ConsultaModels
                {
                    // you probably would fetch those from the database 
                    Cursos = (lista).Select(c => new SelectListItem { Value = c.idCurso.ToString(), Text = c.nombreCurso }),
                    encargadoCurso ="",
                };
                return View(model);
            }
            
        } 


        //
        // GET: /Consulta/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Consulta/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Consulta/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Consulta/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Consulta/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Consulta/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Consulta/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
